﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lon.Util;
using System.Security.Policy;

namespace Lon.Server
{
    public partial class UserManForm : Form
    {
        public UserManForm()
        {
            InitializeComponent();
        }



        protected override void OnLoad(EventArgs e)
        {
           
            ShowPrivilegeItems();
            SetPrivilegeState(PrivilegeType.NONE);

            UpdateDataSource();

            base.OnLoad(e);
        }

        protected override void OnClientSizeChanged(EventArgs e)
        {
            ResizeGrid();
            base.OnClientSizeChanged(e);
        }

        void UpdateDataSource()
        {
            //显示数据
            checkDirty = false;
            preRow = null;

            DataTable dt = QueryUserTable();
            if (dt != null)
            {
                dataGridView1.DataSource = dt;
                ResizeGrid();
            }
        }

        private DataTable QueryUserTable()
        {
            throw new NotImplementedException();
        }

        void ResizeGrid()
        {
            //调整DataGridView列宽
            if (dataGridView1.DataSource != null)
            {
                DataTable dt = (DataTable)dataGridView1.DataSource;

                int cellWidth = (this.dataGridView1.ClientRectangle.Width - dt.Columns.Count) / dt.Columns.Count;

                for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
                {
                    this.dataGridView1.Columns[i].Width = cellWidth;
                }

            }
        }

        #region 权限显示与设置
        bool checkDirty = false;
        DataGridViewRow preRow = null;

        void ShowPrivilegeItems()
        {
            //显示状态
            foreach (PrivilegeType item in PrivilegeMan.PrivilegeList)
            {
                string text = EnumHelper.GetEnumDisString(item.GetType(),(int)item);
                
                TreeNode node = treeView1.Nodes.Add(text);
                
                node.Tag = item;

            }
        }

        void SetPrivilegeState(PrivilegeType value)
        {
            //设置状态
            foreach (TreeNode node in treeView1.Nodes)
            {
                PrivilegeType type = (PrivilegeType)node.Tag;
         
                bool flag = ((type & value) != 0);

                node.Checked = flag;
            }

            checkDirty = false;
        }

        PrivilegeType GetPrivilegeState()
        {
            //获取状态
            PrivilegeType comType = PrivilegeType.NONE;

            foreach (TreeNode node in treeView1.Nodes)
            {
                PrivilegeType type = (PrivilegeType)node.Tag;

                if (node.Checked)
                {
                    comType |= type;
                }
            }

            return comType;
        }


        void SavePrivilegeState()
        {
            //保存状态
            if (preRow == null) return;
            
            PrivilegeType type = GetPrivilegeState();
            
            int level = (int)type;
            
            preRow.Cells[0].Value = level;

            string userName = preRow.Cells[1].Value.ToString();            

            UpdateUserLevel(userName, level);

            checkDirty = false;

        }

        private void UpdateUserLevel(string userName, int level)
        {
          
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            checkDirty = true;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1 || dataGridView1.SelectedRows[0].IsNewRow) return;

            DataGridViewRow dataRow = dataGridView1.SelectedRows[0];

            if (checkDirty && preRow != null)
            {
                //提示是否保存，改变的项
                if (MessageBox.Show(" 权限设置已改变，是否保存?   ", "保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    SavePrivilegeState();
                }
            }

            int value = Convert.ToInt32(dataRow.Cells[0].Value);

            SetPrivilegeState((PrivilegeType)value);

            preRow = dataRow;
            
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            //全选中
            foreach (TreeNode node in treeView1.Nodes)
            {
                node.Checked = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //全取消
            foreach (TreeNode node in treeView1.Nodes)
            {
                node.Checked = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //保存
            SavePrivilegeState();
        }

        #endregion


        #region 主菜单
        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuAddUser_Click(object sender, EventArgs e)
        {
            UserEditForm userEditForm = new UserEditForm();
            userEditForm.AddUserMode();
            userEditForm.Level = 0;
            if (userEditForm.ShowDialog() == DialogResult.OK)
            {
                //添加新用户
                string userName = userEditForm.UserName;
                string password = userEditForm.Password;
                int level = userEditForm.Level;
                string memo = userEditForm.Memo;

                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                {
                    MessageBox.Show(" 用户名或密码为空！   ", "无效输入", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                password = HashHelper.GetTextHash(password);
                AddUser(level, userName, password, DateTime.Now, DateTime.Now.AddYears(50), memo);

                UpdateDataSource();
            }

        }

        private void AddUser(int level, string userName, string password, DateTime dateTime, DateTime dateTime_5, string memo)
        {
           
        }

        private void mnuDelUser_Click(object sender, EventArgs e)
        {
            //删除用户
            if (dataGridView1.SelectedRows.Count < 1 || dataGridView1.SelectedRows[0].IsNewRow) return;
            DataGridViewRow dataRow = dataGridView1.SelectedRows[0];

            string userName = dataRow.Cells[1].Value.ToString();
            if (MessageBox.Show(string.Format(" 即将删除用户 [{0}]，是否继续?   ", userName), "删除用户", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
               DelUser(userName);
                UpdateDataSource();
            }

        }

        private void DelUser(string userName)
        {
            
        }

        private void mnuRefresh_Click(object sender, EventArgs e)
        {
            //刷新
            UpdateDataSource();
        }

        private void mnuEditPassword_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1 || dataGridView1.SelectedRows[0].IsNewRow) return;
            DataGridViewRow dataRow = dataGridView1.SelectedRows[0];

            UserEditForm userEditForm = new UserEditForm();
            userEditForm.EditPasswordMode();
            userEditForm.Level = Convert.ToInt32(dataRow.Cells[0].Value);
            userEditForm.UserName = dataRow.Cells[1].Value.ToString();
            userEditForm.Password = "";
            userEditForm.Memo = dataRow.Cells[3].Value.ToString();

            if (userEditForm.ShowDialog() == DialogResult.OK)
            {
                //修改密码
                string password = userEditForm.Password;

                if (string.IsNullOrEmpty(password))
                {
                    MessageBox.Show(" 密码为空！   ", "无效输入", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                password = HashHelper.GetTextHash(password);
                UpdateUserPassword(userEditForm.UserName, password);
                
                MessageBox.Show(" 密码修改完毕！   ", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                UpdateDataSource();
            }

        }

        private void UpdateUserPassword(string p, string password)
        {
            throw new NotImplementedException();
        }

        private void mnuEditMemo_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1 || dataGridView1.SelectedRows[0].IsNewRow) return;
            DataGridViewRow dataRow = dataGridView1.SelectedRows[0];

            UserEditForm userEditForm = new UserEditForm();
            userEditForm.EditMemoMode();
            userEditForm.Level = Convert.ToInt32(dataRow.Cells[0].Value);
            userEditForm.UserName = dataRow.Cells[1].Value.ToString();
            userEditForm.Password = "";
            userEditForm.Memo = dataRow.Cells[3].Value.ToString();

            if (userEditForm.ShowDialog() == DialogResult.OK)
            {
                //修改备注
                UpdateUserMemo(userEditForm.UserName, userEditForm.Memo);
                UpdateDataSource();
            }
        }

        private void UpdateUserMemo(string p, string p_2)
        {
            
        }

        #endregion


    }

}
